﻿using System;
using System.Text;
using System.Windows.Media;

namespace OpenLS.Spreadsheet.UI
{
    public class ActualHeaderFooter
    {
        private readonly string _text;
        private string _centerText;
        private FontFamily _fontFamily;
        // private readonly WorksheetPage _page;
        private string _leftText;
        private string _rightText;

        private ActualHeaderFooter(string text, Func<char, string> fn, FontFamily fontFamily)
        {
            this._fontFamily = fontFamily;
            _text = text;
            Action<string> action = null;
            var sb = new StringBuilder();
            int i = 0;

            while (i < text.Length)
            {
                char c = text[i];
                if (c == '&')
                {
                    if (i < text.Length - 1)
                    {
                        char next = text[i + 1];
                        switch (next)
                        {
                            case 'L':
                                if (action != null)
                                    action(sb.ToString());
                                sb = new StringBuilder();
                                action = (s) => _leftText = s;
                                break;
                            case 'C':
                                if (action != null)
                                    action(sb.ToString());
                                sb = new StringBuilder();
                                action = (s) => _centerText = s;
                                break;
                            case 'R':
                                if (action != null)
                                    action(sb.ToString());
                                sb = new StringBuilder();
                                action = (s) => _rightText = s;
                                break;

                            default:
                                sb.Append(fn(next));
                                break;
                        }
                        i++;
                    }
                }
                else
                {
                    sb.Append(text[i]);
                }
                i++;
            }
            if (action != null)
                action(sb.ToString());
            //_page = page;
        }

        public string LeftText
        {
            get { return _leftText; }
        }

        public string CenterText
        {
            get { return _centerText; }
        }

        public string RightText
        {
            get { return _rightText; }
        }

        public FontFamily FontFamily
        {
            get { return _fontFamily; }
        }


        internal static ActualHeaderFooter Parse(string text, Func<char, string> page, FontFamily fontFamily)
        {
            if (text == null)
                return null;
            return new ActualHeaderFooter(text, page, fontFamily);
        }
    }
}